<?php

$file = $argv[1];
if(!file_exists($file) || !is_file($file)){
    die("Usage: {$argv[0]} filename!\n");
}

$string = preg_replace_callback(
        '~<\?\s*
            (include|url|\$)\s*\(?(.*?)\)?
        \s*\?>~x',
        function($matches){
            switch($matches[1]){
            case 'url':
                return "<{url a={$matches[2]}}>";
            case '$':
                $pos = strpos($matches[2], '=');
                if($pos!==false){
                    if(preg_match('~^(\w+)\s*=\s*([\'"])(.*)\2$~', $matches[2], $submatch)){
                        return "<{assign var={$submatch[1]} value={$submatch[2]}{$submatch[3]}{$submatch[2]}}>";
                    }
                    else{
                        return $matches[0];
                    }
                }
                else{
                    return "<{\${$matches[2]}}>";
                }
            case 'include':
                return "<{include file={$matches[2]}}>";
            }
        },
        file_get_contents($file)
);

file_put_contents($file, $string);
//echo "$string\n";

?>
